package com.ecar.api.service.driver.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecar.api.service.driver.CarService;
import com.ecar.common.dto.DataBean;
import com.ecar.core.common.dao.DictionaryMapper;
import com.ecar.core.common.entity.Dictionary;
import com.ecar.core.driver.dao.CarMapper;
import com.ecar.core.driver.dao.RecommendCarMapper;
import com.ecar.core.driver.entity.Car;
import com.ecar.core.driver.entity.RecommendCar;

import net.sf.json.JSONObject;

@Service
@Transactional
public class CarServiceImpl implements CarService{
	@Autowired
	DictionaryMapper dictionaryMapper;
	
	@Autowired
	CarMapper carMapper;
	
	@Autowired
	RecommendCarMapper recommendCarMapper;

	@Override
	public int deleteByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return carMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Car record) {
		// TODO Auto-generated method stub
		return carMapper.insert(record);
	}

	@Override
	public int insertSelective(Car record) {
		// TODO Auto-generated method stub
		return carMapper.insertSelective(record);
	}

	@Override
	public DataBean selectByDriverId(String driverid) {
		Car car=carMapper.selectByDriverId(driverid);
		if(car==null){
			return new DataBean(404,new JSONObject(),"尚未分配车辆");
		}
		car.setSeatType(car.getSeatType()==1?5:7);
		return new DataBean(200,car,"查询成功");
	}

	@Override
	public int updateByPrimaryKeySelective(Car record) {
		// TODO Auto-generated method stub
		return carMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Car record) {
		// TODO Auto-generated method stub
		return carMapper.updateByPrimaryKey(record);
	}

	@Override
	public DataBean selectByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Car selectCarByDriverId(String driverid) {
		return carMapper.selectByDriverId(driverid);
	}

	@Override
	public List<Object> selectAllCarType() {
		ArrayList<Object> list1 = new ArrayList<>();
		List<Dictionary> list = dictionaryMapper.selectAllCarType();
		for (Dictionary dictionary : list) {
			HashMap<Object, Object> map = new HashMap<>();
			map.put("carLevel", dictionary.getOrdyBy());
			map.put("carType", dictionary.getName());
			list1.add(map);
		}
		
		return list1;
	}

	@Override
	public List<RecommendCar> selectAllRecommendCar() {
		return recommendCarMapper.selectAllRecommendCar();
	}

}
